package com.g0dkar.leet.core.configuration.loaders;

import java.util.Map.Entry;
import java.util.Properties;

import com.g0dkar.leet.core.configuration.LeetConfiguration;
import com.g0dkar.leet.core.model.loaders.ModelLoaderInterface;
import com.g0dkar.leet.util.ognl.OgnlUtils;

public class LeetConfigurationPropertiesLoader implements ModelLoaderInterface<LeetConfiguration, Properties> {
	private LeetConfiguration configuration;
	
	public LeetConfigurationPropertiesLoader() {
		configuration = null;
	}
	
	public LeetConfigurationPropertiesLoader(LeetConfiguration configuration) {
		this.configuration = configuration;
	}

	public LeetConfiguration load(Properties properties) {
		configuration = new LeetConfigurationClassLoader().load(configuration);
		
		String key;
		for (Entry<Object, Object> entry : properties.entrySet()) {
			key = entry.getKey().toString();
			if (key.startsWith("conf.") && key.length() > 5) {
				OgnlUtils.set(key.substring(5), entry.getValue(), configuration);
			}
		}
		
		return configuration;
	}
	
}
